"""
Case Type   : 基础功能
Case Name   : 发布端订阅端表不一致
Create At   : 2022/2/7
Owner       : opentestcase026
Description :
    1.创建表
    2.创建发布订阅
    3.集群B创建表后创建订阅
    4.表中插入数据
    5.查询数据是否更新
    6.订阅端修改表结构
    7.更新数据
    8.查询数据是否更新
    9.发布端修改表结构
    10.插入数据
    11.查询数据是否更新
    12.订阅端修改表结构
    13.插入数据
    14.查询数据是否更新
    15.订阅端修改表结构
    16.更新数据
    17.查询数据是否更新
    18.修改表结构，字段类型不一致
    19.更新数据
    20.查询数据是否更新
Expect      :
    1.成功
    2.创建发布成功，创建订阅失败报错表不存在
    3.成功
    4.成功
    5.成功,且(1, 1, 1)被同步
    6.成功
    7.成功
    8.更新，但新增数据为(7,77,777,'')
    9.成功
    10.成功
    11.更新
    12.成功
    13.成功
    14.未同步，订阅端日志报错FATAL:  logical replication target relation "xxx"
    is missing some replicated columns
    15.成功
    16.成功
    17.更新(200,200,200,200, 'last')
    18.成功
    19.成功
    20.未同步，订阅端提示FATAL:  invalid input syntax for type xxx: "xxx"
History     :
    modified：2022-4-6 by 5328126;修改pg_log目录，避免其他日志影响
    modified：2022-11-18 by wx1162244;对订阅端不创建表，创建订阅会报错表不存在
    增加断言，订阅端创建表后，创建订阅成功
"""
import unittest
import os
from yat.test import macro
from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class Pubsubclass(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info("-----------this is setup-----------")
        self.log.info("-Opengauss_Function_Pub_Sub_Case0058 start-")
        self.pri_userdb_pub = Node(node='PrimaryDbUser')
        self.pri_userdb_sub = Node(node='remote1_PrimaryDbUser')
        self.constant = Constant()
        self.commsh_pub = CommonSH('PrimaryDbUser')
        self.commsh_sub = CommonSH('remote1_PrimaryDbUser')
        self.com_pub = Common()
        self.com_sub = Common('remote1_PrimaryDbUser')
        self.tb_name1 = 'tb_pubsub_case058_1'
        self.subname1 = "sub_case058_1"
        self.pubname1 = "pub_case058_1"
        self.parent_path_pub = os.path.dirname(macro.DB_INSTANCE_PATH)
        self.parent_path_sub = os.path.dirname(macro.DB_INSTANCE_PATH_REMOTE1)
        self.port = str(int(self.pri_userdb_pub.db_port) + 1)
        self.wal_level = self.com_pub.show_param("wal_level")
        self.user_param_pub = f'-U {self.pri_userdb_pub.db_user} ' \
            f'-W {self.pri_userdb_pub.db_password}'
        self.user_param_sub = f'-U {self.pri_userdb_sub.db_user} ' \
            f'-W {self.pri_userdb_sub.db_password}'

        cmd = f"cp " \
            f"{os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')} " \
            f"{os.path.join(self.parent_path_pub, 'pg_hba.conf')};"
        self.log.info(cmd)
        result = self.pri_userdb_pub.sh(cmd).result()
        self.log.info(result)
        cmd = f"cp " \
            f"{os.path.join(macro.DB_INSTANCE_PATH_REMOTE1, 'pg_hba.conf')}" \
            f" {os.path.join(self.parent_path_sub, 'pg_hba.conf')};"
        self.log.info(cmd)
        result = self.pri_userdb_sub.sh(cmd).result()
        self.log.info(result)
        self.case_no = os.path.basename(__file__)[-6:-3]
        self.dir_new_sub = os.path.join('$GAUSSLOG', 'pg_log', 'pg_bak',
                                        f'pub_sub_case{self.case_no}')
        self.log_directory_s = self.com_sub.show_param(
            "log_directory", macro.DB_ENV_PATH_REMOTE1)
        self.hostname_s = self.pri_userdb_sub.sh('hostname').result().strip()

    def test_pubsub(self):
        text = '--step:预置条件,修改pg_hba expect:成功'
        self.log.info(text)
        guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host    replication  {self.pri_userdb_sub.db_user} '
            f'{self.pri_userdb_sub.db_host}/32 sha256')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败:' + text)
        result = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, 'wal_level=logical')
        self.assertTrue(result, '执行失败:' + text)
        result = self.commsh_pub.restart_db_cluster(True)
        flg = self.constant.START_SUCCESS_MSG in result or 'Degrade' in result
        self.assertTrue(flg, '执行失败:' + text)
        guc_res = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host    replication  {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_pub.db_host}/32 sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败:' + text)
        result = self.commsh_sub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f"log_directory='{self.dir_new_sub}'",
            self.hostname_s, False, False,
            macro.DB_INSTANCE_PATH_REMOTE1, '',
            macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(result, '执行失败:' + text)
        result = self.commsh_sub.restart_db_cluster(True,
                                                    macro.DB_ENV_PATH_REMOTE1)
        flg = self.constant.START_SUCCESS_MSG in result or 'Degrade' in result
        self.assertTrue(flg, '执行失败:' + text)

        text = '--step1:创建表 expect:成功--'
        self.log.info(text)
        create_tb = f"CREATE TABLE {self.tb_name1}" \
            f"(id1 INT primary key, id2 INT, id3 INT);"
        result = self.commsh_pub.execut_db_sql(
            create_tb, sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertEqual(result.count(self.constant.TABLE_CREATE_SUCCESS),
                         2, '执行失败:' + text)

        text = "--step2:创建发布订阅 expect:创建发布成功，创建订阅报错--"
        self.log.info(text)
        create_pub = f"CREATE PUBLICATION {self.pubname1} for all tables ;"
        result = self.commsh_pub.execut_db_sql(create_pub,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.create_pub_succ_msg, result,
                      '执行失败:' + text)
        self.assertNotIn(self.constant.SQL_WRONG_MSG[1], result,
                         '执行失败:' + text)

        result = self.commsh_sub.execute_generate(
            macro.COMMON_PASSWD, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertIn('', result, '执行失败:' + text)
        create_sub = f"CREATE SUBSCRIPTION {self.subname1} CONNECTION " \
            f"'host={self.pri_userdb_pub.db_host} " \
            f"port={self.port} " \
            f"user={self.pri_userdb_pub.db_user} " \
            f"dbname={self.pri_userdb_pub.db_name} " \
            f"password={self.pri_userdb_pub.ssh_password}' " \
            f"PUBLICATION {self.pubname1};"
        result = self.commsh_sub.execut_db_sql(create_sub, self.user_param_sub,
                                               None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        err = f'ERROR:  relation "public.{self.tb_name1}" does not exist'
        self.assertEqual(err, result, '执行失败:' + text)

        text = "--step3:集群B创建表后创建订阅 expect:成功--"
        self.log.info(text)
        tb_result = self.commsh_sub.execut_db_sql(
            create_tb, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(tb_result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS,
                      tb_result, '执行失败:' + text)
        sub_result = self.commsh_sub.execut_db_sql(
            create_sub, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn(self.constant.create_sub_succ_msg, sub_result,
                      '执行失败:' + text)

        text = "--step4:插入数据 expect:成功--"
        self.log.info(text)
        sql = f"insert into {self.tb_name1} values(1, 1, 1);" \
              f"select pg_sleep(20);"
        result = self.commsh_pub.execut_db_sql(sql,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      result, '执行失败:' + text)

        text = "--step5:查询数据是否更新 expect:数据同步 1，1，1--"
        self.log.info(text)
        sub_select = f"select * from {self.tb_name1};"
        result = self.commsh_sub.execut_db_sql(sub_select, self.user_param_sub,
                                               None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn('1 row', result, '执行失败:' + text)
        self.assertIn('1 |   1 |   1', result, '执行失败:' + text)

        text = "--step6:订阅端修改表结构 expect:成功--"
        self.log.info(text)
        sql_alt = f"alter table {self.tb_name1} add COLUMN i int;"
        result = self.commsh_sub.execut_db_sql(sql_alt, self.user_param_sub,
                                               None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn(self.constant.ALTER_TABLE_MSG,
                      result, '执行失败:' + text)

        text = "--step7:更新数据 expect:成功--"
        self.log.info(text)
        sql = f"update {self.tb_name1} set id3 = 999;" \
            f"insert into {self.tb_name1} values(7, 77, 777);"
        result = self.commsh_pub.execut_db_sql(sql,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      result, '执行失败:' + text)
        self.assertIn(self.constant.UPDATE_SUCCESS_MSG,
                      result, '执行失败:' + text)

        text = "--step8:查询数据是否更新 expect:更新，新增(7,77,777,'')--"
        self.log.info(text)
        result = self.commsh_sub.execut_db_sql(sub_select, self.user_param_sub,
                                               None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn('2 rows', result, '执行失败:' + text)
        self.assertIn('7 |  77 | 777 |', result, '执行失败:' + text)
        self.assertIn('1 |   1 | 999 |', result, '执行失败:' + text)

        text = "--step9:发布端修改表结构 expect:成功--"
        self.log.info(text)
        result = self.commsh_pub.execut_db_sql(sql_alt,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.ALTER_TABLE_MSG, result, '执行失败:' + text)

        text = "--step10:插入数据 expect:成功--"
        self.log.info(text)
        sql = f"insert into {self.tb_name1} values(99, 9,9, 9);"
        result = self.commsh_pub.execut_db_sql(sql,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      result, '执行失败:' + text)

        text = "--step11:查询数据是否更新 expect:更新--"
        self.log.info(text)
        result = self.commsh_sub.execut_db_sql(sub_select, self.user_param_sub,
                                               None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn('3 rows', result, '执行失败:' + text)
        self.assertIn('99 |   9 |   9 | 9', result, '执行失败:' + text)

        text = "--step12:订阅端修改表结构 expect:成功--"
        self.log.info(text)
        sql_alt = f"alter table {self.tb_name1} " \
                  f"add COLUMN last varchar(100);"
        result = self.commsh_pub.execut_db_sql(sql_alt,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.ALTER_TABLE_MSG, result, '执行失败:' + text)

        text = "--step13:插入数据 expect:成功--"
        self.log.info(text)
        sql = f"insert into {self.tb_name1} values(999,9,9,9, 'last');"
        result = self.commsh_pub.execut_db_sql(sql,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      result, '执行失败:' + text)

        text = "--step14:查询数据是否更新 expect:未同步，订阅端日志报错" \
               "FATAL:  logical replication target relation \"xxx\" " \
               "is missing some replicated columns--"
        self.log.info(text)
        result = self.commsh_sub.execut_db_sql(sub_select, self.user_param_sub,
                                               None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn('3 rows', result, '执行失败:' + text)
        self.assertNotIn('last', result, '执行失败:' + text)
        flg = f"FATAL:  logical replication target relation"
        result = self.com_pub.find_pglog_content(
            node=self.pri_userdb_sub,
            content=flg,
            env_path=macro.DB_ENV_PATH_REMOTE1,
            path=self.dir_new_sub)
        self.assertTrue(result, '执行失败:' + text)

        text = "--step15:订阅端修改表结构 expect:成功--"
        self.log.info(text)
        sql_alt = f"alter table {self.tb_name1} add COLUMN last text;" \
            f"select pg_sleep(30);"
        result = self.commsh_sub.execut_db_sql(sql_alt, self.user_param_sub,
                                               None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn(self.constant.ALTER_TABLE_MSG, result, '执行失败:' + text)

        text = "--step16:更新数据 expect:成功--"
        self.log.info(text)
        sql = f"insert into {self.tb_name1} values(200,200,200,200, 'last');" \
            f"select pg_sleep(30);"
        result = self.commsh_pub.execut_db_sql(sql,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      result, '执行失败:' + text)

        text = "--step17:查询数据是否更新 " \
               "expect:更新(200,200,200,200, 'last')--"
        self.log.info(text)
        result = self.commsh_sub.execut_db_sql(sub_select, self.user_param_sub,
                                               None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn('5 rows', result, '执行失败:' + text)
        self.assertIn('200 | 200 | 200 | 200 | last', result, '执行失败:' + text)
        self.assertIn('999 |   9 |   9 |   9 | last', result, '执行失败:' + text)

        text = "--step18:修改表结构，字段类型不一致 expect:成功--"
        self.log.info(text)
        sql_alt = f"alter table {self.tb_name1} add COLUMN dif int;"
        result = self.commsh_sub.execut_db_sql(sql_alt,
                                               self.user_param_sub,
                                               None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn(self.constant.ALTER_TABLE_MSG, result, '执行失败:' + text)
        sql_alt = f"alter table {self.tb_name1} add COLUMN  dif text;"
        result = self.commsh_pub.execut_db_sql(sql_alt,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.ALTER_TABLE_MSG, result, '执行失败:' + text)

        text = "--step19:更新数据 expect:成功--"
        self.log.info(text)
        sql = f"insert into {self.tb_name1} values" \
            f"(300,300,300,300, 'last', 'dif');"
        result = self.commsh_pub.execut_db_sql(sql,
                                               sql_type=self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      result, '执行失败:' + text)

        text = "--step20:查询数据是否更新 expect:未同步，" \
               "订阅端提示FATAL:  invalid input syntax for type --"
        self.log.info(text)
        result = self.commsh_sub.execut_db_sql(sub_select, self.user_param_sub,
                                               None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn('5 rows', result, '执行失败:' + text)
        self.assertNotIn('300 | 300 | 300 | 300 | last | diff',
                         result, '执行失败:' + text)
        flg = f"FATAL:  invalid input syntax for integer"
        result = self.com_pub.find_pglog_content(
            node=self.pri_userdb_sub,
            content=flg,
            env_path=macro.DB_ENV_PATH_REMOTE1,
            path=self.dir_new_sub)
        self.assertTrue(result, '执行失败:' + text)

    def tearDown(self):
        self.log.info('------------this is tearDown-------------')
        text = '--清理环境--'
        self.log.info(text)
        sql = f"DROP PUBLICATION if exists {self.pubname1};"
        drop_pub_result = self.commsh_pub.execut_db_sql(
            sql, sql_type=self.user_param_pub)
        self.log.info(drop_pub_result)
        sql = f"DROP SUBSCRIPTION  {self.subname1};"
        drop_sub_result = self.commsh_sub.execut_db_sql(
            sql, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(drop_sub_result)
        sql = f"DROP table if exists {self.tb_name1};"
        result = self.commsh_sub.execut_db_sql(sql, self.user_param_sub, None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        result = self.commsh_pub.execut_db_sql(sql,
                                               sql_type=self.user_param_pub)
        self.log.info(result)

        cmd = f"mv " \
            f"{os.path.join(self.parent_path_pub, 'pg_hba.conf')} "\
            f"{os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')};"
        self.log.info(cmd)
        result = self.pri_userdb_pub.sh(cmd).result()
        self.log.info(result)
        cmd = f"mv " \
            f"{os.path.join(self.parent_path_sub, 'pg_hba.conf')} "\
            f"{os.path.join(macro.DB_INSTANCE_PATH_REMOTE1, 'pg_hba.conf')} "
        self.log.info(cmd)
        result = self.pri_userdb_sub.sh(cmd).result()
        self.log.info(result)
        result = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'wal_level={self.wal_level}')
        result1 = self.commsh_sub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f"log_directory='{self.log_directory_s}'",
            self.hostname_s, False, False,
            macro.DB_INSTANCE_PATH_REMOTE1, '',
            macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(result and result1, '执行失败:' + text)
        self.commsh_pub.restart_db_cluster(True)
        self.commsh_sub.restart_db_cluster(True, macro.DB_ENV_PATH_REMOTE1)
        self.assertIn(self.constant.drop_pub_succ_msg, drop_pub_result,
                      '执行失败' + text)
        self.assertIn(self.constant.drop_sub_succ_msg, drop_sub_result,
                      '执行失败' + text)
        self.log.info("-Opengauss_Function_Pub_Sub_Case0058 end-")
